//
//  YXHomeCarMenuView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/21.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXHomeCarMenuView.h"

@interface YXHomeCarMenuView ()
@property (nonatomic ,strong) UIView *backView;
@property (nonatomic ,strong) UIView *bLineView;
@property (nonatomic ,strong) UILabel *titleLab;
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) NSArray *imageArr;
@end

@implementation YXHomeCarMenuView

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:(CGRectMake(10, 0, kScreenWidth - 20, 140))];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UIView *)bLineView {
    if (!_bLineView) {
        _bLineView = [[UIView alloc] init];
        _bLineView.backgroundColor = [UIColor py_colorWithHexString:@"#4BC3FB"];
    }
    return _bLineView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor blackColor];
    }
    return _titleLab;
}

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title withBtnTitleArr:(NSArray *)btnTitleArr withBtnImageArr:(NSArray *)btnImageArrr;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArr = btnTitleArr;
        self.imageArr = btnImageArrr;
        [self createUI:title];
    }
    return self;
}

- (void)createUI:(NSString *)title {
    [self addSubview:self.backView];
    [self.backView addSubview:self.bLineView];
    self.titleLab.text = title;
    [self.backView addSubview:self.titleLab];
    
    for (int i = 0; i < self.titleArr.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(self.backView.frame.size.width/self.titleArr.count*i, 40, self.backView.frame.size.width/self.titleArr.count, 60);
        //        btn.backgroundColor = [UIColor randomColor];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArr[i]]] forState:(UIControlStateNormal)];
        [btn setTitle:self.titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:color_textOne forState:(UIControlStateNormal)];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.tag = 300 + i;
        [btn setImgViewStyle:(ButtonImgViewStyleTop) imageSize:(CGSizeMake(60, 60)) space:8];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.backView addSubview:btn];
     
    }
    
    for (int i = 0 ; i < self.titleArr.count; i ++) {
        // 分割线
        if (i > 0 && self.frame.size.height > 16) {
            UIView *line = [[UIView alloc] initWithFrame:(CGRectMake(self.backView.W / self.titleArr.count *i, 40, 1, self.frame.size.height - 58))];
            line.backgroundColor = color_lineColor;
            [self.backView addSubview:line];
        }
    }
    
}

- (void)btnAction:(UIButton *)sender {
    if (self.selectClickBtnBlock) {
        self.selectClickBtnBlock(sender.tag - 300,sender.titleLabel.text);
    }
    
}


- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_bLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.backView.mas_left).offset(15);
        make.top.equalTo(weakSelf.backView.mas_top).offset(15);
        make.size.mas_equalTo(CGSizeMake(2, 14));
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.bLineView.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.bLineView.mas_centerY);
        [_titleLab sizeToFit];
    }];
}


@end
